package com.gxa.pingan.service.impl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.UserInsureMapper;
import com.gxa.pingan.mapper.UserMapper;
import com.gxa.pingan.pojo.User;
import com.gxa.pingan.pojo.UserInsure;
import com.gxa.pingan.service.UserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 注意不要在service中互相调用
 *
 * @author YXQ
 * @date 2021/10/18 10:41
 */
@Service(value = "userService")
@Transactional(rollbackFor = {Throwable.class})
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserInsureMapper userInsureMapper;

    /**
     * 通过用户的id号查询用户
     *
     * @param id 用户的id号
     * @return 用户对象
     * @throws BusinessException
     */
    @Override
    public Map<String, Object> getUserById(Long id) throws BusinessException {
        Map<String, Object> map = userMapper.selectById(id);
        // 设置时间格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Object insureTime = map.get("insureTime");
        // 按照格式将时间转换成对应格式的字符串
        String format = dateFormat.format(insureTime);
        // 截取年月日，时分秒，去除毫秒
        String substring = format.substring(0, 19);
        map.put("insureTime",substring);
        if (map == null) {
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        return map;
    }

    /**
     * 添加用户
     *
     * @param user 用户对象
     * @throws BusinessException
     */
    @Override
    public void addUser(Map<String,Object> user) throws BusinessException {
        User getUser = userMapper.selectByPhone(user.get("phone").toString());
        User userByIdCard = userMapper.selectByIdCard(user.get("idCard").toString());
        UserInsure userInsure = userInsureMapper.selectByPhone(user.get("phone").toString());
        if (userByIdCard!=null){
            throw new BusinessException(ExceptionCode.HAS_ID_CARD);
        }
        if (getUser!=null){
            throw new BusinessException(ExceptionCode.HAS_USER);
        }
        if (userInsure==null){
            userInsureMapper.insert(user);
        }else {
            userInsureMapper.update(user);
        }

        userMapper.addUser(user);
    }

    /**
     * 获取全部用户列表
     *
     * @return
     * @throws BusinessException
     */
    @Override
    public Integer getList() throws BusinessException {
        List<User> list = userMapper.getList();
        // 先判断是否为空
        if (list.size() == 0) {
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        return list.size();
    }

    /**
     * 导出全部用户
     *
     * @return
     * @throws BusinessException
     */
    @Override
    public List<User> upload() throws BusinessException {
        List<User> list = userMapper.getList();
        if (list.size()==0){
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        return list;
    }

    /**
     * 跟叶获取全部用户列表
     *
     * @param page 页码
     * @param size 每页大小
     * @return 返回分页列表
     * @throws BusinessException
     */
    @Override
    public List<User> getListByPage(Integer page, Integer size) throws BusinessException {
        List<User> listByPage = userMapper.getListByPage((page - 1) * size, size);
        if (listByPage.size() == 0) {
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        return listByPage;
    }

    /**
     * 修改用户信息
     *
     * @param user 用户对象
     * @throws BusinessException
     */
    @Override
    public void update(Map<String,Object> user) throws BusinessException {
        Map<String, Object> map = userMapper.selectById(Long.valueOf(user.get("id").toString()));
        UserInsure userInsure = userInsureMapper.selectByPhone(user.get("oldPhone").toString());
        if (userInsure==null){
            userInsureMapper.insert(user);
        }else {
            userInsureMapper.update(user);
        }
        // 先确保用户存在
        if (map==null){
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        userMapper.update(user);
    }

    /**
     * 根据搜索框的值查找
     *
     * @param value
     * @return
     * @throws BusinessException
     */
    @Override
    public Integer search(String value) throws BusinessException {
        List<User> search = userMapper.getSearch(value);
        if (search.size()==0){
            throw new BusinessException(ExceptionCode.NO_SUCH);
        }
        return search.size();
    }

    /**
     * 分页搜索用户列表
     * @param value 搜索框的值
     * @param page 页码
     * @param size 每页长度
     * @return
     * @throws BusinessException
     */
    @Override
    public List<User> getSearch(String value, Integer page, Integer size) throws BusinessException {
        List<User> search = userMapper.getSearch(value);
        if (search.size()==0){
            throw new BusinessException(ExceptionCode.NO_SUCH);
        }
        List<User> searchByPage = userMapper.getSearchByPage(value, (page-1)*size, size);
        return searchByPage;
    }

    @Override
    public User findByIdCard(String idCard) {
        return userMapper.selectByIdCard(idCard);
    }


}
